package whu.iss.system.tools;


import java.math.BigDecimal;
import java.security.MessageDigest;


public class Tools 
{
	private Tools(){}
	
	
	public static int getCount(String str) 
	{
		int count=0;
		char[] c1=str.toCharArray();
		for(int i=0;i<c1.length;i++) 
		{
			if(Character.toString(c1[i]).matches("[\\u4E00-\\u9FA5]+")) 
			{
				count++;
			}
			
		}
		return count;
	}
	
	
	//**************************BEGIN MD5*************************************
	
	 public static String getMd5(Object pwd) throws Exception
	 {
		 String m1=Tools.MD5Encode(pwd.toString());
		 String pwd2=m1+"蒲公英dandelionодуванчикLöwenzahnดอกแดนดิไลอัน"+m1;
		 String m2=Tools.MD5Encode(pwd2);
		 
		 
		 return m2;
	 }
	
	 private final static String[] hexDigits = {
		     "0", "1", "2", "3", "4", "5", "6", "7",
		     "8", "9", "a", "b", "c", "d", "e", "f"};

		  private static String byteArrayToHexString(byte[] b)
		  {
		      StringBuffer resultSb = new StringBuffer();
		      for (int i = 0; i < b.length; i++)
		      {
		         resultSb.append(byteToHexString(b[i]));
		      }
		      return resultSb.toString();
		  }

		  private static String byteToHexString(byte b)
		  {
		      int n = b;
		      if (n < 0)
		         n = 256 + n;
		      int d1 = n / 16;
		      int d2 = n % 16;
		      return hexDigits[d1] + hexDigits[d2];
		  }

		  public static String MD5Encode(String origin) throws Exception
		  {
		       String resultString = null;
		       try
		       {
		           resultString=new String(origin);
		           MessageDigest md = MessageDigest.getInstance("MD5");
		           resultString=byteArrayToHexString(md.digest(resultString.getBytes()));
		           return resultString;
		       }
		       catch (Exception ex)
		       {
		          throw ex;
		       }
		  }
		//******************END  MD5******************

	
	



	private final static int MATCH_SCALE=2;         //��������Ĭ��С��λ��

	public static double ObjToDouble(Object dol, int scale)
	{
		  return Tools.ObjectToBigDecimal(dol, scale).doubleValue();
	}
	public static double ObjToDouble(Object dol)
	{
	   return Tools.ObjToDouble(dol, MATCH_SCALE);	
	}
	
	public static String DoubleToStr(double dol, int scale)
	{
	    return Tools.ObjectToBigDecimal(dol, scale).toString();
	}
	public static String DoubleToStr(double dol)
	{
	    return Tools.DoubleToStr(dol, MATCH_SCALE);
	}

	public static double DoubleToDouble(double dol, int scale)
	{
	    return Tools.ObjectToBigDecimal(dol, scale).doubleValue();
	}
	public static double DoubleToDouble(double dol)
	{
	    return Tools.DoubleToDouble(dol,  MATCH_SCALE);
	}

	public static double StrToDouble(String dol, int scale)
	{
	    return Tools.ObjectToBigDecimal(dol, scale).doubleValue();
	}
	public static double StrToDouble(String dol)
	{
	    return Tools.StrToDouble(dol, MATCH_SCALE);
	}
	public static String StrToStr(String dol, int scale)
	{
	   return Tools.ObjectToBigDecimal(dol, scale).toString();
	}
	public static String StrToStr(String dol)
	{
	  return Tools.StrToStr(dol,MATCH_SCALE);
	}
	
	private static BigDecimal ObjectToBigDecimal(Object dol,int scale)
	{
		BigDecimal decimal=null;
		if(dol==null || dol.equals(""))
		{
			return new BigDecimal(0);
		}
		decimal = new BigDecimal(dol.toString());
		decimal = decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return decimal;
	}

}
